'''
这个程序与ReadLog.py大致相同，不同之处是使用sched包设置定时
'''
import sched, time
from openpyxl import load_workbook


class ReadLogUp:
    log_path = 'C:/Users/qaz24/Desktop/duxiaoman.log'
    book = load_workbook(filename='D:/codes/Python/job/AppAutoTest/log.xlsx')
    st = book.worksheets[0]
    s = sched.scheduler(time.time, time.sleep)
    row_location = 2
    F_num = 0
    E_num = 0
    W_num = 0
    I_num = 0
    D_num = 0

    def timedreadlog(self, sc):
        with open(self.log_path, 'r', encoding='utf-8') as f:
            lines = f.readlines()
            for line in lines:
                if len(line) > 2:
                    if line[0] == 'F' and line[1] == '/':
                        self.F_num += 1
                    elif line[0] == 'E' and line[1] == '/':
                        self.E_num += 1
                    elif line[0] == 'W' and line[1] == '/':
                        self.W_num += 1
                    elif line[0] == 'I' and line[1] == '/':
                        self.I_num += 1
                    elif line[0] == 'D' and line[1] == '/':
                        self.D_num += 1
            f.flush()
        print(self.st)
        self.st.cell(self.row_location, 1).value = self.F_num
        self.F_num = 0
        self.st.cell(self.row_location, 2).value = self.E_num
        self.E_num = 0
        self.st.cell(self.row_location, 3).value = self.W_num
        self.W_num = 0
        self.st.cell(self.row_location, 4).value = self.I_num
        self.I_num = 0
        self.st.cell(self.row_location, 5).value = self.D_num
        self.D_num = 0
        self.book.save('D:/codes/Python/job/AppAutoTest/log.xlsx')
        self.row_location += 1
        # 在程序流程结束后，再次安排运行，实现循环
        self.s.enter(2, 1, self.timedreadlog, (sc,))


readlog = ReadLogUp()
# 安排第一次运行，第一个参数是时间间隔（以秒为单位），第二个参数是优先级（较低的数字表示较高的优先级），
# 第三个参数是要运行的函数，第四个参数是要传递给该函数的参数。
readlog.s.enter(2, 1, readlog.timedreadlog, (readlog.s,))
# 启动调度器，一定要有
readlog.s.run()
